package com.jun.common.redis.config;

import com.google.common.base.Charsets;
import com.google.common.hash.Funnel;
import com.jun.common.redis.helper.BloomFilterHelper;
import com.jun.common.redis.utils.BloomRedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.PostConstruct;

/**
 * @author yuejun.li
 * @date 2024/7/15 15:49:41
 * 布隆过滤器配置
 */
@Slf4j
@Configuration
public class BloomFilterConfig {

    @Autowired
    private RedisTemplate redisTemplate;

    @PostConstruct
    public void init(){
        //可以在这里写一开始就加载到布隆过滤器的逻辑
    }

    @Bean
    public BloomFilterHelper<String> initBloomFilterHelper() {
        return new BloomFilterHelper<>((Funnel<String>) (from, into) -> into.putString(from, Charsets.UTF_8)
                .putString(from, Charsets.UTF_8), 1000000, 0.01);
    }

    // 布隆过滤器bean注入
    @Bean
    public BloomRedisUtil bloomRedisUtil(){
        BloomRedisUtil bloomRedisUtil = new BloomRedisUtil();
        bloomRedisUtil.setBloomFilterHelper(initBloomFilterHelper());
        bloomRedisUtil.setRedisTemplate(redisTemplate);
        return bloomRedisUtil;
    }
}
